﻿using System;
using System.Web.Security;

namespace ActivateYourGlutes.Infrastructure.MembershipServices
{
    public class AccountMembershipService : IMembershipService
    {
        private readonly MembershipProvider _provider;
        private readonly RoleProvider _roleProvider;


        public AccountMembershipService()
            : this(null, null)
        {
        }

        public AccountMembershipService(MembershipProvider provider, RoleProvider roleProvider)
        {
            _provider = provider ?? Membership.Provider;
            _roleProvider = roleProvider ?? System.Web.Security.Roles.Provider;
        }

        public int MinPasswordLength
        {
            get
            {
                return _provider.MinRequiredPasswordLength;
            }
        }

        public bool ValidateUser(string userName, string password)
        {
            return _provider.ValidateUser(userName, password);
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            _provider.CreateUser(userName, password, email, null, null, true, null, out status);
            if (status == MembershipCreateStatus.Success)
            {
                try
                {
                    _roleProvider.AddUsersToRoles(new[] { userName }, new[] { Roles.User });
                }
                catch (Exception)
                {
                    _provider.DeleteUser(userName, true);
                    throw;
                }
            }
            return status;
        }

        public bool DeleteUser(string userName)
        {
            return _provider.DeleteUser(userName, true);
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
            return currentUser.ChangePassword(oldPassword, newPassword);
        }

        public bool ChangeEmail(string username, string email)
        {
            MembershipUser currentUser = _provider.GetUser(username, true);
            currentUser.Email = email;
            _provider.UpdateUser(currentUser);
            return true;
        }

        public string Email(string username)
        {
            MembershipUser currentUser = _provider.GetUser(username, true);
            return currentUser.Email;
        }

        public bool IsAdministrator(string username)
        {
            return _roleProvider.IsUserInRole(username, Roles.Administrator);
        }

        public void SetIsAdministrator(string username, bool administrator)
        {
            if (!administrator)
            {
                _roleProvider.RemoveUsersFromRoles(new[] {username}, new[] {Roles.Administrator});
            }
            else
            {
                _roleProvider.AddUsersToRoles(new[] {username}, new[] {Roles.Administrator});
            }
        }
    }
}
